﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Web.UI.WebControls;
using nkstdgia.common.HoSoThamDinh;
using wsRK.AppUser;
using wsRK.ConvertionHelper;
using wsRK.Web;

namespace nkstdgia.main
{
    public partial class dashboard : WebPage
    {
        #region Variables
        private HoSoThamDinhDataService _hstdController = null;
        private string _soPhieuChinh = "";
        private Boolean _disableColums = false;
        #endregion Variables

        #region Properties

        public HoSoThamDinhDataService hstdController { get { return _hstdController == null ? new HoSoThamDinhDataService() : _hstdController; } }

        private bool _blnSoftTimeAsc
        {
            get { return ViewState["_blnSoftTimeAsc"] == null ? false : (bool)ViewState["_blnSoftTimeAsc"]; }
            set { ViewState["_blnSoftTimeAsc"] = value; }
        }

        private bool _blngiadatAsc
        {
            get { return ViewState["_blngiadatAsc"] == null ? true : (bool)ViewState["_blngiadatAsc"]; }
            set { ViewState["_blngiadatAsc"] = value; }
        }

        private bool _blngia1Asc
        {
            get { return ViewState["_blngia1Asc"] == null ? true : (bool)ViewState["_blngia1Asc"]; }
            set { ViewState["_blngia1Asc"] = value; }
        }

        private bool _blngia2Asc
        {
            get { return ViewState["_blngia2Asc"] == null ? true : (bool)ViewState["_blngia2Asc"]; }
            set { ViewState["_blngia2Asc"] = value; }
        }

        private bool _blnstatusAsc
        {
            get { return ViewState["_blnstatusAsc"] == null ? true : (bool)ViewState["_blnstatusAsc"]; }
            set { ViewState["_blnstatusAsc"] = value; }
        }

        private bool _blnphieusoAsc
        {
            get { return ViewState["_blnphieusoAsc"] == null ? true : (bool)ViewState["_blnphieusoAsc"]; }
            set { ViewState["_blnphieusoAsc"] = value; }
        }

        private bool _blnGiaGiaoDichAsc
        {
            get { return ViewState["_blnGiaGiaoDichAsc"] == null ? true : (bool)ViewState["_blnGiaGiaoDichAsc"]; }
            set { ViewState["_blnGiaGiaoDichAsc"] = value; }
        }

        #endregion Properties

        #region Construction

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this._getData(10, 1);
            }
        }

        #endregion Construction

        #region get data

        private void _getData(int pageSize, int pageNo)
        {
            List<HoSoThamDinhInfo> lshs = _getListData(pageSize, pageNo, "");

            if (lshs != null)
            {
                if (lshs.Count > 0)
                {
                    int totalRecord = ConvertHelper.toInt(lshs[0].RowTotal);
                    this.lvDSKhaoSat.PageIndex = pageNo;
                    this.lvDSKhaoSat.PageSize = pageSize;
                    this.lvDSKhaoSat.TotalRecord = totalRecord;

                    this.lvDSKhaoSat.DataSource = lshs;
                    this.lvDSKhaoSat.DataBind();
                }
                else
                {
                    this.lvDSKhaoSat.DataSource = null;
                    this.lvDSKhaoSat.DataBind();
                }
            }
        }

        private List<HoSoThamDinhInfo> _getListData(int pageSize, int pageNo, string orderType)
        {
            HoSoThamDinhDataService hstdController = new HoSoThamDinhDataService();
            MembershipUser obj = MembershipDataservice.getUser(User.Identity.Name);
            List<HoSoThamDinhInfo> lshs = hstdController.searchHoSoThamDinhByOwner(
                new Guid(obj.ProviderUserKey.ToString()),
                "",
                pageSize,
                pageNo);

            switch (orderType)
            {
                case "timeAsc":
                    lshs = (from hs in lshs
                            orderby hs.NGAY_KHOI_TAO ascending
                            select hs).ToList();
                    break;
                case "timeDes":
                    lshs = (from hs in lshs
                            orderby hs.NGAY_KHOI_TAO descending
                            select hs).ToList();
                    break;
                case "giadatAsc":
                    lshs = (from hs in lshs
                            orderby hs.GIA_DAT ascending
                            select hs).ToList();
                    break;
                case "giadatDes":
                    lshs = (from hs in lshs
                            orderby hs.GIA_DAT descending
                            select hs).ToList();
                    break;
                case "gia1Des":
                    lshs = (from hs in lshs
                            orderby hs.GIA_DAT_1 descending
                            select hs).ToList();
                    break;
                case "gia1Asc":
                    lshs = (from hs in lshs
                            orderby hs.GIA_DAT_1 ascending
                            select hs).ToList();
                    break;
                case "gia2Des":
                    lshs = (from hs in lshs
                            orderby hs.GIA_DAT_2 descending
                            select hs).ToList();
                    break;
                case "gia2Asc":
                    lshs = (from hs in lshs
                            orderby hs.GIA_DAT_2 ascending
                            select hs).ToList();
                    break;
                case "statusDes":
                    lshs = (from hs in lshs
                            orderby hs.ACTIVE_FLAG descending
                            select hs).ToList();
                    break;
                case "statusAsc":
                    lshs = (from hs in lshs
                            orderby hs.ACTIVE_FLAG ascending
                            select hs).ToList();
                    break;
                case "phieusoDes":
                    lshs = (from hs in lshs
                            orderby hs.SO_PHIEU descending
                            select hs).ToList();
                    break;
                case "phieusoAsc":
                    lshs = (from hs in lshs
                            orderby hs.SO_PHIEU ascending
                            select hs).ToList();
                    break;
                default:
                    break;
            }
            return lshs;
        }

        private void _bindData(List<HoSoThamDinhInfo> lshs)
        {
            if (lshs != null)
            {
                if (lshs.Count > 0)
                {
                    int totalRecord = ConvertHelper.toInt(lshs[0].RowTotal);
                    this.lvDSKhaoSat.TotalRecord = totalRecord;

                    this.lvDSKhaoSat.DataSource = lshs;
                    this.lvDSKhaoSat.DataBind();
                }
                else
                {
                    this.lvDSKhaoSat.DataSource = null;
                    this.lvDSKhaoSat.DataBind();
                }
            }
            base.hideLoader();
        }

        #endregion get data

        #region Event

        protected void sortbyDateTime_click(object sender, EventArgs e)
        {
            List<HoSoThamDinhInfo> lshs = null;
            if (_blnSoftTimeAsc)
            {
                lshs = this._getListData(this.lvDSKhaoSat.PageSize, this.lvDSKhaoSat.PageIndex, "timeAsc");
                _blnSoftTimeAsc = false;
            }
            else
            {
                lshs = this._getListData(this.lvDSKhaoSat.PageSize, this.lvDSKhaoSat.PageIndex, "timeDes");
                _blnSoftTimeAsc = true;
            }
            this._bindData(lshs);
        }

        protected void sortbyGiaDat_click(object sender, EventArgs e)
        {
            List<HoSoThamDinhInfo> lshs = null;
            if (_blngiadatAsc)
            {
                lshs = this._getListData(this.lvDSKhaoSat.PageSize, this.lvDSKhaoSat.PageIndex, "giadatAsc");
                _blngiadatAsc = false;
            }
            else
            {
                lshs = this._getListData(this.lvDSKhaoSat.PageSize, this.lvDSKhaoSat.PageIndex, "giadatDes");
                _blngiadatAsc = true;
            }
            _bindData(lshs);
        }

        protected void sortbyGiaGiaoDich_click(object sender, EventArgs e)
        {
            List<HoSoThamDinhInfo> lshs = null;
            if (_blnGiaGiaoDichAsc)
            {
                lshs = this._getListData(this.lvDSKhaoSat.PageSize, this.lvDSKhaoSat.PageIndex, "giaGiaoDichAsc");
                _blnGiaGiaoDichAsc = false;
            }
            else
            {
                lshs = this._getListData(this.lvDSKhaoSat.PageSize, this.lvDSKhaoSat.PageIndex, "giaGiaoDichDes");
                _blnGiaGiaoDichAsc = true;
            }
            _bindData(lshs);
        }

        protected void sortbyGia1_click(object sender, EventArgs e)
        {
            List<HoSoThamDinhInfo> lshs = null;
            if (_blngia1Asc)
            {
                lshs = this._getListData(this.lvDSKhaoSat.PageSize, this.lvDSKhaoSat.PageIndex, "gia1Asc");
                _blngia1Asc = false;
            }
            else
            {
                lshs = this._getListData(this.lvDSKhaoSat.PageSize, this.lvDSKhaoSat.PageIndex, "gia1Des");
                _blngia1Asc = true;
            }
            this._bindData(lshs);
        }

        protected void sortbyGia2_click(object sender, EventArgs e)
        {
            List<HoSoThamDinhInfo> lshs = null;
            if (_blngia2Asc)
            {
                lshs = this._getListData(this.lvDSKhaoSat.PageSize, this.lvDSKhaoSat.PageIndex, "gia2Asc");
                _blngia2Asc = false;
            }
            else
            {
                lshs = this._getListData(this.lvDSKhaoSat.PageSize, this.lvDSKhaoSat.PageIndex, "gia2Des");
                _blngia2Asc = true;
            }
            this._bindData(lshs);
        }

        protected void sortbyStatus_click(object sender, EventArgs e)
        {
            List<HoSoThamDinhInfo> lshs = null;
            if (_blngia2Asc)
            {
                lshs = this._getListData(this.lvDSKhaoSat.PageSize, this.lvDSKhaoSat.PageIndex, "statusAsc");
                _blngia2Asc = false;
            }
            else
            {
                lshs = this._getListData(this.lvDSKhaoSat.PageSize, this.lvDSKhaoSat.PageIndex, "statusDes");
                _blngia2Asc = true;
            }
            this._bindData(lshs);
        }

        protected void sortbyPhieuso_click(object sender, EventArgs e)
        {
            List<HoSoThamDinhInfo> lshs = null;
            if (_blngia2Asc)
            {
                lshs = this._getListData(this.lvDSKhaoSat.PageSize, this.lvDSKhaoSat.PageIndex, "phieusoAsc");
                _blngia2Asc = false;
            }
            else
            {
                lshs = this._getListData(this.lvDSKhaoSat.PageSize, this.lvDSKhaoSat.PageIndex, "phieusoDes");
                _blngia2Asc = true;
            }
            this._bindData(lshs);
        }
       
        protected void lvDSKhaoSat_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            Image imgMajor = e.Item.FindControl("imgMajor") as Image;
            if (imgMajor != null)
            {
                if (imgMajor.ImageUrl != this._soPhieuChinh)
                {
                    this._soPhieuChinh = imgMajor.ImageUrl;
                    imgMajor.AlternateText = this._soPhieuChinh;
                    imgMajor.ImageUrl = "~/images/bookmarks.png";
                }
                else
                {
                    imgMajor.Visible = false;
                }
            }

            MembershipUser loginUser = MembershipDataservice.getUser(User.Identity.Name);
            if (User.IsInRole("Sinh viên") || loginUser == null)
            {
                System.Web.UI.Control control = e.Item.FindControl("td1GDD") as System.Web.UI.Control;
                if (control != null)
                {
                    control.Visible = false;
                }
                control = e.Item.FindControl("td1GD1") as System.Web.UI.Control;
                if (control != null)
                {
                    control.Visible = false;
                }
                control = e.Item.FindControl("td1GD2") as System.Web.UI.Control;
                if (control != null)
                {
                    control.Visible = false;
                }
                control = e.Item.FindControl("td2GDD") as System.Web.UI.Control;
                if (control != null)
                {
                    control.Visible = false;
                }
                control = e.Item.FindControl("td2GD1") as System.Web.UI.Control;
                if (control != null)
                {
                    control.Visible = false;
                }
                control = e.Item.FindControl("td2GD2") as System.Web.UI.Control;
                if (control != null)
                {
                    control.Visible = false;
                }

                if (!this._disableColums)
                {
                    control = this.lvDSKhaoSat.FindControl("thGDD") as System.Web.UI.Control;
                    if (control != null)
                    {
                        control.Visible = false;
                    }
                    control = this.lvDSKhaoSat.FindControl("thG1") as System.Web.UI.Control;
                    if (control != null)
                    {
                        control.Visible = false;
                    }
                    control = this.lvDSKhaoSat.FindControl("thG2") as System.Web.UI.Control;
                    if (control != null)
                    {
                        control.Visible = false;
                    }
                    control = this.lvDSKhaoSat.FindControl("td3GDD") as System.Web.UI.Control;
                    if (control != null)
                    {
                        control.Visible = false;
                    }
                    control = this.lvDSKhaoSat.FindControl("td3GD1") as System.Web.UI.Control;
                    if (control != null)
                    {
                        control.Visible = false;
                    }
                    control = this.lvDSKhaoSat.FindControl("td3GD2") as System.Web.UI.Control;
                    if (control != null)
                    {
                        control.Visible = false;
                    }
                    this._disableColums = true;
                }
            }
        }
        #endregion Event

        #region paging

        protected void _lv_btnFirst_Click(object sender, EventArgs e)
        {
            int pageSize = this.lvDSKhaoSat.PageSize;
            int pageNo = 1;
            this._getData(pageSize, pageNo);
        }

        protected void _lv_btnLast_Click(object sender, EventArgs e)
        {
            int pageSize = this.lvDSKhaoSat.PageSize;
            int pageNo = this.lvDSKhaoSat.PageTotal;
            this._getData(pageSize, pageNo);
        }

        protected void _lv_btnNext_Click(object sender, EventArgs e)
        {
            int pageSize = this.lvDSKhaoSat.PageSize;
            int pageNo = (this.lvDSKhaoSat.PageIndex == this.lvDSKhaoSat.PageTotal) ? this.lvDSKhaoSat.PageIndex : this.lvDSKhaoSat.PageIndex + 1;
            this._getData(pageSize, pageNo);
        }

        protected void _lv_btnPrev_Click(object sender, EventArgs e)
        {
            int pageSize = this.lvDSKhaoSat.PageSize;
            int pageNo = (this.lvDSKhaoSat.PageIndex == 1) ? 1 : this.lvDSKhaoSat.PageIndex - 1;
            this._getData(pageSize, pageNo);
        }

        protected void _lv_ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            int pageSize = Convert.ToInt32((sender as DropDownList).SelectedValue);
            int pageNo = 1;
            this._getData(pageSize, pageNo);
        }

        protected void _lv_ddlPageIndex_SelectedIndexChanged(object sender, EventArgs e)
        {
            int pageSize = this.lvDSKhaoSat.PageSize;
            int pageNo = Convert.ToInt32((sender as DropDownList).SelectedValue);
            this._getData(pageSize, pageNo);
        }

        #endregion paging
    }
}